<template>
  <div class="container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form :inline="true" ref="form" :model="form" label-width="80px" @submit.native.prevent>
          <el-form-item label="学科名称">
            <el-input v-model="form.subjectName" size="small"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button size="small" @click="form.subjectName = ''">取消</el-button>
            <el-button type="primary" size="small" @click="onSubmit">搜索</el-button>
          </el-form-item>
        </el-form>
        <el-button class="addBtn" icon="el-icon-edit" type="text" @click="dialogFormVisible = true">新增学科</el-button>
        <el-alert show-icon :closable="false" :title="`数据一共${totals}条`" type="info"> </el-alert>
      </div>
      <div class="clearfix">
        <subject-list :tableData="tableData" @onChange="onChangeFn" @delete="deleteFn" v-model="isShow"></subject-list>
        <div class="block">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :hide-on-single-page="value"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="pagesize"
            layout=" sizes, prev, pager, next, jumper"
            :total="totals">
          </el-pagination>
        </div>
      </div>
      <div class="confirm">
        <el-dialog title="新增学科" :visible.sync="dialogFormVisible">
          <subjects-add @close="dialogFormVisible = false" @addSubject="addSubject"></subjects-add>
        </el-dialog>
        <el-dialog title="修改学科" :visible.sync="dialogFormVisible2">
          <subject-edit :subjectObj="subjectObj" @close="closeFn" v-if="isShow" @editSubject="editSubject"></subject-edit>
        </el-dialog>
      </div>
    </el-card>
  </div>
</template>

<script>
import { formatTime } from '@/utils/index.js'
import { list, add, remove, update } from '@/api/hmmm/subjects.js'
import subjectList from '../components/subject-list.vue'
import SubjectsAdd from '../components/subjects-add.vue'
import SubjectEdit from '../components/subject-edit.vue'
export default {
  name: 'subject',
  data () {
    return {
      form: {
        subjectName: '',
        newSubjectName: '',
        value: 0
      },
      tableData: [],
      value: false,
      totals: 0,
      pagesize: 10,
      currentPage: 1,
      currentpagesize: 1,
      isdisbleft: true,
      isdisbright: true,
      dialogFormVisible: false, // 控制新增学科弹出框是否显示
      subjectObj: {},
      dialogFormVisible2: false,
      isShow: false
    }
  },
  components: { subjectList, SubjectsAdd, SubjectEdit },
  created () {
    this.getSubjectInfo()
  },
  methods: {
    async getSubjectInfo (pagesize, currentpagesize, subjectName = null) {
      try {
        const res = await list({ pagesize, page: currentpagesize, subjectName })
        this.tableData = res.data.items
        this.totals = res.data.counts
        formatTime(this.tableData.addDate)
      } catch (error) {
        this.$message.error('数据获取失败')
      }
    },
    async handleSizeChange (value) {
      console.log(value)
      this.pagesize = value
      this.currentpagesize = 1
      this.getSubjectInfo(this.pagesize, this.currentpagesize)
      if (this.totals <= this.pagesize) {
        return (this.value = true)
      }
    },
    onSubmit () {
      if (this.form.subjectName === '') {
        return this.getSubjectInfo()
      }
      this.getSubjectInfo(this.pagesize, this.currentpagesize, this.form.subjectName)
      if (this.dialogFormVisible2 === true) {
        return this.editSubject()
      } else {
        this.getSubjectInfo(this.pagesize, this.currentpagesize, this.form.subjectName)
      }
    },
    async handleCurrentChange (value) {
      this.getSubjectInfo(this.pagesize, value)
    },
    async addSubject (value) {
      try {
        const res = await add({
          subjectName: value.newSubjectName,
          isFrontDisplay: value.value
        })
        if (res.data.id) {
          this.getSubjectInfo()
        }
        // this.form.newSubjectName = ''
        this.dialogFormVisible = false
      } catch (error) {
        this.$message.error('添加学科失败')
      }
    },
    closeFn () {
      this.dialogFormVisible2 = false
      this.isShow = false
    },
    async editSubject (value) {
      try {
        await update({
          id: this.subjectObj.id,
          subjectName: value.newSubjectName,
          isFrontDisplay: value.value
        })
        this.getSubjectInfo(this.pagesize, this.currentpagesize, this.form.subjectName)
        this.$message({
          message: '恭喜你,修改成功',
          type: 'success'
        })
        this.dialogFormVisible2 = false
        this.isShow = false
      } catch (error) {
        this.$message.error('修改数据失败')
      }
    },
    onChangeFn (value) {
      this.subjectObj = value
      this.form.newSubjectName = this.subjectObj.subjectName
      this.dialogFormVisible2 = true
    },
    deleteFn (value) {
      console.log(value)
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove({ id: value.id })
          this.getSubjectInfo()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<<<<<<< HEAD
<style scoped lang="scss">
.container {
  background-color: #fff;
  ::v-deep .box-card {
    margin: 15px 8px 8px 8px;
    .addBtn {
      float: right;
      padding: 3px 0;
      margin-top: -60px;
      color: #fff;
      background: #67c23a;
      font-size: 12px;
      padding: 9px 15px;
    }
    .el-card__header {
      border-bottom: none;
    }
    .el-button--small {
      font-size: 15px;
    }
    .block {
      float: right;
      margin-top: 15px;
    }
    .el-dialog {
      border-radius: 10px;
      width: 400px;
      .el-dialog__body {
        background-color: #fff;
      }
    }
  }
  ::v-deep .el-form-item__content {
    .el-input__inner {
      width: 250px;
    }
  }
}
</style>
=======
<style scoped lang='scss'></style>
>>>>>>> 215e2860be06d72afccb886a22f21f14ff59f66c
